﻿using System;

namespace OrderManagement
{
    class Program
    {
        static void Main(string[] args)
        {
            var service = new OrderService();
            bool exit = false;

            while (!exit)
            {
                Console.WriteLine("\n=== 订单管理系统 ===");
                Console.WriteLine("1. 添加订单");
                Console.WriteLine("2. 删除订单");
                Console.WriteLine("3. 修改订单");
                Console.WriteLine("4. 查询订单");
                Console.WriteLine("5. 导出订单到XML");
                Console.WriteLine("6. 从XML导入订单");
                Console.WriteLine("7. 显示所有订单");
                Console.WriteLine("8. 排序订单");
                Console.WriteLine("0. 退出");
                Console.Write("请选择操作: ");

                if (int.TryParse(Console.ReadLine(), out int choice))
                {
                    try
                    {
                        switch (choice)
                        {
                            case 1:
                                AddOrder(service);
                                break;
                            case 2:
                                DeleteOrder(service);
                                break;
                            case 3:
                                UpdateOrder(service);
                                break;
                            case 4:
                                QueryOrders(service);
                                break;
                            case 5:
                                ExportOrders(service);
                                break;
                            case 6:
                                ImportOrders(service);
                                break;
                            case 7:
                                DisplayOrders(service.GetAllOrders());
                                break;
                            case 8:
                                SortOrders(service);
                                break;
                            case 0:
                                exit = true;
                                Console.WriteLine("感谢使用订单管理系统！");
                                break;
                            default:
                                Console.WriteLine("无效选择，请重新输入。");
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"错误: {ex.Message}");
                    }
                }
                else
                {
                    Console.WriteLine("输入无效，请输入数字。");
                }
            }
        }

        static void AddOrder(OrderService service)
        {
            Console.Write("请输入订单号: ");
            string orderId = Console.ReadLine();
            Console.Write("请输入客户名称: ");
            string customer = Console.ReadLine();

            var order = new Order(orderId, customer);

            bool addMoreDetails = true;
            while (addMoreDetails)
            {
                Console.Write("请输入商品名称: ");
                string productName = Console.ReadLine();
                Console.Write("请输入数量: ");
                int quantity = int.Parse(Console.ReadLine());
                Console.Write("请输入单价: ");
                double unitPrice = double.Parse(Console.ReadLine());

                order.AddDetails(new OrderDetails(productName, quantity, unitPrice));

                Console.Write("是否继续添加明细？(y/n): ");
                addMoreDetails = Console.ReadLine().ToLower() == "y";
            }

            service.AddOrder(order);
            Console.WriteLine("订单添加成功！");
        }

        static void DeleteOrder(OrderService service)
        {
            Console.Write("请输入要删除的订单号: ");
            string orderId = Console.ReadLine();
            service.DeleteOrder(orderId);
            Console.WriteLine("订单删除成功！");
        }

        static void UpdateOrder(OrderService service)
        {
            Console.Write("请输入要修改的订单号: ");
            string orderId = Console.ReadLine();

            var existingOrder = service.QueryByOrderId(orderId).FirstOrDefault();
            if (existingOrder == null)
            {
                throw new ArgumentException($"订单不存在: {orderId}");
            }

            Console.Write($"请输入新客户名称(当前: {existingOrder.Customer}): ");
            string customer = Console.ReadLine();
            if (!string.IsNullOrEmpty(customer))
            {
                existingOrder.Customer = customer;
            }

            bool updateDetails = true;
            while (updateDetails)
            {
                Console.WriteLine("1. 添加明细");
                Console.WriteLine("2. 删除明细");
                Console.WriteLine("0. 完成修改");
                Console.Write("请选择操作: ");
                int choice = int.Parse(Console.ReadLine());

                switch (choice)
                {
                    case 1:
                        Console.Write("请输入商品名称: ");
                        string productName = Console.ReadLine();
                        Console.Write("请输入数量: ");
                        int quantity = int.Parse(Console.ReadLine());
                        Console.Write("请输入单价: ");
                        double unitPrice = double.Parse(Console.ReadLine());
                        existingOrder.AddDetails(new OrderDetails(productName, quantity, unitPrice));
                        break;
                    case 2:
                        Console.Write("请输入要删除的商品名称: ");
                        string productToRemove = Console.ReadLine();
                        existingOrder.Details.RemoveAll(d => d.ProductName == productToRemove);
                        break;
                    case 0:
                        updateDetails = false;
                        break;
                    default:
                        Console.WriteLine("无效选择");
                        break;
                }
            }

            service.UpdateOrder(existingOrder);
            Console.WriteLine("订单修改成功！");
        }

        static void QueryOrders(OrderService service)
        {
            Console.WriteLine("1. 按订单号查询");
            Console.WriteLine("2. 按商品名称查询");
            Console.WriteLine("3. 按客户查询");
            Console.WriteLine("4. 按金额范围查询");
            Console.Write("请选择查询方式: ");
            int queryType = int.Parse(Console.ReadLine());

            List<Order> result = null;

            switch (queryType)
            {
                case 1:
                    Console.Write("请输入订单号: ");
                    result = service.QueryByOrderId(Console.ReadLine());
                    break;
                case 2:
                    Console.Write("请输入商品名称: ");
                    result = service.QueryByProductName(Console.ReadLine());
                    break;
                case 3:
                    Console.Write("请输入客户名称: ");
                    result = service.QueryByCustomer(Console.ReadLine());
                    break;
                case 4:
                    Console.Write("请输入最小金额: ");
                    double min = double.Parse(Console.ReadLine());
                    Console.Write("请输入最大金额: ");
                    double max = double.Parse(Console.ReadLine());
                    result = service.QueryByAmountRange(min, max);
                    break;
                default:
                    Console.WriteLine("无效选择");
                    return;
            }

            DisplayOrders(result);
        }

        static void DisplayOrders(List<Order> orders)
        {
            if (orders == null || orders.Count == 0)
            {
                Console.WriteLine("没有找到订单。");
                return;
            }

            Console.WriteLine("\n查询结果:");
            foreach (var order in orders)
            {
                Console.WriteLine(order);
                Console.WriteLine(new string('-', 50));
            }
        }

        static void ExportOrders(OrderService service)
        {
            Console.Write("请输入导出文件路径: ");
            string filePath = Console.ReadLine();
            service.Export(filePath);
            Console.WriteLine($"订单已导出到 {filePath}");
        }

        static void ImportOrders(OrderService service)
        {
            Console.Write("请输入导入文件路径: ");
            string filePath = Console.ReadLine();
            service.Import(filePath);
            Console.WriteLine($"订单已从 {filePath} 导入");
        }

        static void SortOrders(OrderService service)
        {
            Console.WriteLine("1. 按订单号排序(默认)");
            Console.WriteLine("2. 按金额排序");
            Console.WriteLine("3. 按日期排序");
            Console.Write("请选择排序方式: ");
            int sortType = int.Parse(Console.ReadLine());

            switch (sortType)
            {
                case 1:
                    service.SortOrders();
                    break;
                case 2:
                    service.SortOrders((x, y) => y.TotalAmount.CompareTo(x.TotalAmount));
                    break;
                case 3:
                    service.SortOrders((x, y) => y.OrderDate.CompareTo(x.OrderDate));
                    break;
                default:
                    Console.WriteLine("无效选择，使用默认排序");
                    service.SortOrders();
                    break;
            }

            Console.WriteLine("订单已排序");
            DisplayOrders(service.GetAllOrders());
        }
    }
}